package net.capstone.cra.smartcontroller.motion;


/**
 * The Class ShakeMotion.
 */
public class ShakeMotion extends Motion {
	
	/**
	 * The last time.
	 */
	private long lastTime;
    
    /**
	 * The speed.
	 */
    private float speed;
    
    /**
	 * The last x.
	 */
    private float lastX;
    
    /**
	 * The last y.
	 */
    private float lastY;
    
    /**
	 * The last z.
	 */
    private float lastZ;
    
    /**
	 * The z.
	 */
    private float x, y, z;
    
    /**
	 * The Constant SHAKE_THRESHOLD.
	 */
    private static final int SHAKE_THRESHOLD = 530;
	
	/**
	 * Instantiates a new shake motion.
	 */
	public ShakeMotion() {
		setType(Motion.TYPE_SHAKE);
	}
	
	/* (non-Javadoc)
	 * @see net.capstone.cra.smartcontroller.motion.Motion#detect(net.capstone.cra.smartcontroller.motion.AccelerometerEvent)
	 */
	public boolean detect(AccelerometerEvent event) {
		long currentTime = System.currentTimeMillis();
        long gabOfTime = (currentTime - lastTime);

        if (gabOfTime > 120) {
            lastTime = currentTime;

            x = event.accX;
            y = event.accY;
            z = event.accZ;

            speed = Math.abs(x + y + z - lastX - lastY - lastZ) /
                    gabOfTime * 10000;

            lastX = event.accX;
            lastY = event.accY;
            lastZ = event.accZ;

            if (speed > SHAKE_THRESHOLD) {
               return true;
            }
        } 
        
        return false;
	}
}

